06. Smart Pointer Explanation
Smart Pointer in Garbage Collector
Description
When it comes to reference counting, every memory block that is dynamically allocated has a reference count tied to it. This reference count gets incremented by 1 when a reference is created in the memory, and also decremented by 1, when a reference is deleted from the memory. In other words, when a pointer gets placed to point to a certain allocated memory block the reference count for that memory block is increased by 1. Likewise, when the pointer gets redirected to another memory block, the reference count is decremented by 1.
In the situation where the reference count gets to the value of zero, that indicated that the memory is not being used, therefore, it can be deallocated. The biggest advantage of reference counting is the fact that it is simple. It is not hard to understand or implement. In addition, it does not put any restrictions on the organization of the heap due to the fact that the reference count is not dependant of the physical location of objects. One thing that reference counting adds is some amount of overhead to every pointer operation. However it needs to be said that the collection phase has a somewhat low cost.
This principle is encapsulated and used in std::shared_ptr. In this class of data we can use these pointers multiple times and they are going to "delete themselves" without any memory leakage, but in reality all of that principle is implemented over reference counting.
Reference Counting in Garbage Collector
In order to implement a garbage collector with reference counting, there has to be some method of keeping track of how many pointers are pointing to how many allocated memory blocks. The problem with C++ is that it doesn't have any pre-implemented way of knowing and tracking how and when an object is pointing to another object. The way to solve this problem is by creating a new custom modified pointer type that will be able to support garbage collecting. In order for this to work, three aspects of the modified pointer have to be implemented. The first thing is that the pointer has to keep track of the reference counts for all the active dynamically allocated object in the form of some list. The second thing now, apart from maintaining this list, it also has to keep track of the pointer operations. This means it has to increment and decrement the reference counts as the pointers point to various memory blocks. Lastly, the objects that achieve a reference count of zero have to be recycled.
This newly created pointer will apear like any other regular pointer when it is used, apart from the fact that it will support garbage collencting. Pointer operations like * and -> are also supported by this pointer type. The implementation of a custom garbage collection pointer is good in the sense where if it is needed, it can be used. If garbage collection is not needed, than the regular pointers can be used. The option of using pointers in the same program is also possible.
Having all of the things stated above in mind, we are going to tackle problem of reference counting by developing our own smart pointer. This pointer is going to be developed to reflect same internal techique as mentioned shared_ptr. With this we can get extensive understanding of smart pointers in general and how they use C++ syntax, options and logic to prevent memory leakage. When we are finish we can use our pointers almost as typical integrated pointers in C++.